package com.example.k.littletag.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.k.littletag.R;

/**
 * Created by KongFanyang on 2015/10/5.
 */
public class PreActivity extends Activity {
    public static final String CHECK_COUNT = "check_count";
    public static final String CONTENT_SAVE = "content_save";
    public static final String TIME_PICKED = "time_picked";
    public static final String REWRITE = "rewrite";

    public static final String ARG_DRAWING_START_LOCATION = "drawing_start_location";
    public static final String ARG_REVEAL_START_LOCATION = "reveal_start_location";

    private static final String DELETE_ITEM = "delete_item";
    private int drawingStartLocation;
    private ImageButton deleteButton;

    private ImageButton tvChange;
    private TextView tvAchieve;
    private Button showTimeButton;
    private TextView showContentText;
    private LinearLayout contentRoot;
    private CheckBox checkboxPres_1;
    private CheckBox checkboxPres_2;
    private CheckBox checkboxPres_3;
    private CheckBox checkboxPres_4;
    private CheckBox checkboxPres_5;
    private int checkCount;
    private int deleteItem;
    private int isFinish;

    private int[] time;
    private String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pre_activity);

        initView();
        initActionView();

        drawingStartLocation = getIntent().getIntExtra(ARG_DRAWING_START_LOCATION, 0);
        if (savedInstanceState == null) {
            contentRoot.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    contentRoot.getViewTreeObserver().removeOnPreDrawListener(this);
                    startIntroAnimation();
                    return true;
                }
            });
        }
    }

    private void initView() {

        tvAchieve = (TextView) findViewById(R.id.tv_achieve);
        deleteItem = getIntent().getIntExtra(DELETE_ITEM, -1);
        showTimeButton = (Button) findViewById(R.id.show_time);
        showContentText = (TextView) findViewById(R.id.text_note_show);
        contentRoot = (LinearLayout) findViewById(R.id.content_root);
        tvChange = (ImageButton) findViewById(R.id.edit_button);
        deleteButton = (ImageButton) findViewById(R.id.delete_button);

        isFinish = getIntent().getIntExtra("isFinish", -1);
        time = getIntent().getIntArrayExtra(TIME_PICKED);
        content = getIntent().getStringExtra(CONTENT_SAVE);
        checkCount = getIntent().getIntExtra(CHECK_COUNT, -1);


        showTimeButton.setEnabled(false);


        if (isFinish == 1) {
            tvAchieve.setText("已完成");
            tvAchieve.setEnabled(false);
            tvChange.setVisibility(View.INVISIBLE);
            if (time[0] == 0 && time[1] == 0 && time[2] == 0) {
                showTimeButton.setText("完成");
            } else {
                showTimeButton.setText(time[0] + "-" + time[1] + "-" + time[2] + " " + time[3] + ":" + time[4]);
            }
        } else {
            if (time[0] == 0 && time[1] == 0 && time[2] == 0) {
                showTimeButton.setTextColor(getResources().getColor(R.color.reveal_red));
                showTimeButton.setText("待办");
            }
             else {
                showTimeButton.setText(time[0] + "-" + time[1] + "-" + time[2] + " " + time[3] + ":" + time[4]);
            }
        }

        showContentText.setText(content);

        checkboxPres_1 = (CheckBox) findViewById(R.id.checkbox_1_pre);
        checkboxPres_2 = (CheckBox) findViewById(R.id.checkbox_2_pre);
        checkboxPres_3 = (CheckBox) findViewById(R.id.checkbox_3_pre);
        checkboxPres_4 = (CheckBox) findViewById(R.id.checkbox_4_pre);
        checkboxPres_5 = (CheckBox) findViewById(R.id.checkbox_5_pre);

        switch (checkCount) {
            case 5:
                checkboxPres_1.setVisibility(View.VISIBLE);
                checkboxPres_1.setChecked(true);
            case 4:
                checkboxPres_2.setVisibility(View.VISIBLE);
                checkboxPres_2.setChecked(true);
            case 3:
                checkboxPres_3.setVisibility(View.VISIBLE);
                checkboxPres_3.setChecked(true);
            case 2:
                checkboxPres_4.setVisibility(View.VISIBLE);
                checkboxPres_4.setChecked(true);
            case 1:
                checkboxPres_5.setVisibility(View.VISIBLE);
                checkboxPres_5.setChecked(true);

        }


    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        setResult(RESULT_CANCELED);
    }

    private void initActionView() {
        tvChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int[] location = new int[2];
                v.getLocationInWindow(location);
                Log.d("location in win", location[0] + " " + location[1]);
                location[0] += v.getMeasuredWidth() / 2;
                location[1] += v.getMeasuredHeight() / 2;
                Log.d("location in win", location[0] + " " + location[1]);

                Intent intent = new Intent(getApplication(), SaveNote.class);
                intent.putExtra(ARG_REVEAL_START_LOCATION, location);
                intent.putExtra(CHECK_COUNT, getIntent().getIntExtra(CHECK_COUNT, -1));
                intent.putExtra(TIME_PICKED, getIntent().getIntArrayExtra(TIME_PICKED));
                intent.putExtra(CONTENT_SAVE, getIntent().getStringExtra(CONTENT_SAVE));
                intent.putExtra(REWRITE, 1);
                startActivity(intent);
            }
        });

        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder myDialog = new AlertDialog.Builder(PreActivity.this);
                myDialog.setIcon(R.drawable.ic_delete);
                myDialog.setTitle("提示：");
                myDialog.setMessage("删除本条记录？");
                myDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent();
                        intent.putExtra(DELETE_ITEM, deleteItem);
                        setResult(1, intent);
                        finish();
                    }
                });

                myDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                myDialog.create().show();

            }
        });

        tvAchieve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(DELETE_ITEM, deleteItem);
                setResult(2, intent);
                finish();
            }
        });
    }

    private void startIntroAnimation() {
        contentRoot.setScaleY(0.2f);
        contentRoot.setPivotY(drawingStartLocation);

        contentRoot.animate()
                .scaleY(1)
                .setDuration(200)
                .setInterpolator(new AccelerateInterpolator())
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                    }
                })
                .start();
    }


}
